package no.hin.stg.beans;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import no.hin.stg.db.MysqlAdapter;

/**
 *
 * @author 490501
 */
@ManagedBean
@SessionScoped
public class User implements Serializable {

    private int id, loginTryCount;
    private String name, password, sessionId, message, action;

    public User() {
        LOG.log(Level.INFO, "bean creates :");
    }
    private static final Logger LOG = Logger.getLogger(User.class.getName());

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public int getId() {
        return id;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    /**
     *
     * @return
     */
    public String autentificate() {
        if(this.loginTryCount > 5){
            this.message = "Your account is blocked now";
            return "failed";
        }
        try {
            MysqlAdapter ms = new MysqlAdapter();
            this.id = ms.autentificateUser(this.name, this.password);

            if (this.id > 0) {
                this.sessionId = ((HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false)).getId();
                return "success";
            } else {
                this.loginTryCount +=1;
                this.message = "Access denied";
                return "failed";
            }
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "failed";
    }

    /**
     *
     * @return
     */
    public String logout() {
        ((HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false)).invalidate();
        return "logout";
    }

    public void setLocaleEng() {
        FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale("en"));
        LOG.log(Level.SEVERE, FacesContext.getCurrentInstance().getViewRoot().getLocale().getLanguage());
    }

    public void setLocaleNor() {
        FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale("no"));
        LOG.log(Level.SEVERE, FacesContext.getCurrentInstance().getViewRoot().getLocale().getLanguage());
    }
}
